package com.ruoyi.business.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.business.domain.TOrderInfo;
import com.ruoyi.business.domain.TRecipientInfo;
import com.ruoyi.business.response.ResponseData;
import com.ruoyi.business.service.BasePayment;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.BaiduApiUtil;
import com.ruoyi.common.utils.HttpUtils;
import com.ruoyi.common.utils.MoneyUtil;
import com.ruoyi.common.utils.payment.pbqzpay.PubuPayUtils;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

@Service("pbqzPayService")
public class PbqzPayServiceImpl extends BasePayment {




    @Override
    public JSONObject createPayOrder(TOrderInfo orderInfo) throws Exception {
        JSONObject jsonObject = new JSONObject();
        TRecipientInfo userRecipientInfo = userRecipientService.selectTRecipientInfoById(orderInfo.getRecipientId());
        HashMap<String, String> headerMap = new HashMap();
        HashMap<String, String> dataMap = new HashMap();
//        String qrcode = userRecipientInfo.getReceiptQrcode();
//        String qrcode_ = HttpUtils.getParam(qrcode,"userAppId");

        String accountJson = userRecipientInfo.getAccountJson();
        JSONObject  accountObj = JSONObject.parseObject(accountJson);

        JSONObject ipObject = commonService.getPoxyIp(orderInfo);
        String ip = ipObject.getString("ip");
        String port = ipObject.getString("port");
        logger.info("挂码省份：" + userRecipientInfo.getProvince());
        logger.info("挂码城市：" + userRecipientInfo.getCity());
//        JSONObject ipObject = commonService.getPoxyIp(orderInfo);
        Map location = BaiduApiUtil.getCoordinate(userRecipientInfo.getProvince()+userRecipientInfo.getCity());

        String token = accountObj.getString("token");
        JSONObject  reqOrderObj  = new JSONObject();
        reqOrderObj.put("money", MoneyUtil.fenToYuan(orderInfo.getMoney()+""));
        reqOrderObj.put("payType",0);
        reqOrderObj.put("custPhone","");
        reqOrderObj.put("imei","");
        long time = System.currentTimeMillis()/1000;
        String sign = PubuPayUtils.getSign(time,reqOrderObj,token);
        String submitPayUrl = "http://api.openpubu.com/order/submitFtfpayOrder?token="+token+"&time="+time+"&sign="+sign+"&client_channel=2&api_version=2.2.5";
        String submitPayRes = HttpUtils.sendJsonMessage(submitPayUrl,headerMap,reqOrderObj.toJSONString());
        JSONObject  submitPayResObj = JSONObject.parseObject(submitPayRes);
        if(submitPayResObj.getIntValue("status")!=200){
            if(submitPayResObj.getIntValue("status")==499){
                userRecipientInfo = reLogin(userRecipientInfo);
                submitPayResObj = reSubmit(userRecipientInfo,submitPayResObj);
            }else{
                jsonObject.put("code","100");
                jsonObject.put("msg",submitPayResObj.getString("message"));
                logger.info("========修改收款账号为不可用===============");
                userRecipientInfo.setChannelDisabled(1);
                userRecipientInfo.setDisabledInfo(submitPayResObj.getString("message"));
                userRecipientService.updateTRecipientInfo(userRecipientInfo);
                return jsonObject;
            }
        }
        logger.info("瀑布圈子返回:{}",submitPayResObj.toJSONString());
        JSONObject payDataObj = submitPayResObj.getJSONObject("body");
        String transaction_id = payDataObj.getString("orderId");
        String payUrl = payDataObj.getString("qrcode");
        jsonObject.put("payUrl",payUrl);
        jsonObject.put("orderNo",transaction_id);
        jsonObject.put("code","200");
        return jsonObject;
    }


    public JSONObject createWxPayOrder(TOrderInfo orderInfo) throws ServiceException, UnsupportedEncodingException {
        return null;
    }



    private TRecipientInfo reLogin(TRecipientInfo tRecipientInfo) {
        long time = System.currentTimeMillis()/1000;
        String accountJson = tRecipientInfo.getAccountJson();
        JSONObject  accountObj = JSONObject.parseObject(accountJson);
        HashMap<String, String> headerMap = new HashMap();
        JSONObject  loginObj  = new JSONObject();
        loginObj.put("pwd",accountObj.getString("password"));
        loginObj.put("userName",tRecipientInfo.getChannelAccount());
        String sign = PubuPayUtils.getSign(time,loginObj,"");
        String loginUrl = "http://api.openpubu.com/open/login?token=&time="+time+"&sign="+sign+"&client_channel=2&api_version=2.2.5";
        String loginRes = HttpUtils.sendJsonMessage(loginUrl,headerMap,loginObj.toJSONString());
        JSONObject  loginResObj = JSONObject.parseObject(loginRes);
        if(loginResObj.getIntValue("status")!=200){
            return tRecipientInfo;
        }
        JSONObject userObj = (JSONObject)loginResObj.getJSONObject("body").getJSONArray("userList").get(0);

        String token = userObj.getString("token");
        accountObj.put("token",token);
        tRecipientInfo.setAccountJson(accountObj.toJSONString());
        tRecipientInfo.setOnline(1);
        userRecipientService.updateTRecipientInfo(tRecipientInfo);
        return tRecipientInfo;
    }

    private JSONObject reSubmit(TRecipientInfo tRecipientInfo,JSONObject  reqOrderObj) {
        long time = System.currentTimeMillis()/1000;
        String accountJson = tRecipientInfo.getAccountJson();
        JSONObject  accountObj = JSONObject.parseObject(accountJson);
        HashMap<String, String> headerMap = new HashMap();
        String token = accountObj.getString("token");
        String sign = PubuPayUtils.getSign(time,reqOrderObj,token);
        String submitPayUrl = "http://api.openpubu.com/order/submitFtfpayOrder?token="+token+"&time="+time+"&sign="+sign+"&client_channel=2&api_version=2.2.5";
        String submitPayRes = HttpUtils.sendJsonMessage(submitPayUrl,headerMap,reqOrderObj.toJSONString());
        JSONObject  submitPayResObj = JSONObject.parseObject(submitPayRes);
        return submitPayResObj;
    }

    @Override
    public ResponseData smartReissueOrder(TOrderInfo orderInfo) throws Exception {
        return new ResponseData();
    }
}
